﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using System.Data;
using DTO;
namespace EC21_EDiamond.Admin
{
    public partial class QuanLyDonHang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                LoadDanhSachDonHang();
                LoadThoiGian();
            }
        }
        void LoadThoiGian()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("text");
            dt.Columns.Add("value");
            for (int i = 1; i <= 31; i++)
            {
                DataRow r = dt.NewRow();
                r[0] = "Ngày " +i;
                r[1] = i;
                dt.Rows.Add(r);
            }
            drpNgay.DataSource = dt;
            drpNgay.DataTextField = "text";
            drpNgay.DataValueField = "value";
            drpNgay.DataBind();
            dt.Clear();
            for (int i = 1; i <= 12; i++)
            {
                DataRow r = dt.NewRow();
                r[0] = "Tháng " + i;
                r[1] = i;
                dt.Rows.Add(r);
            }
            drpThang.DataSource = dt;
            drpThang.DataTextField = "text";
            drpThang.DataValueField = "value";
            drpThang.DataBind();
            dt.Clear();
            for (int i = DateTime.Now.Year; i >= DateTime.Now.Year - 10; i--)
            {
                DataRow r = dt.NewRow();
                r[0] = "Năm " + i;
                r[1] = i;
                dt.Rows.Add(r);
            }
            drpNam.DataSource = dt;
            drpNam.DataTextField = "text";
            drpNam.DataValueField = "value";
            drpNam.DataBind();
        }
        void LoadDanhSachDonHang()
        {
            grwDonHang.DataSource =  DonHangBUS.LoadDanhSachDonHang();
            grwDonHang.DataBind();
        }
        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            grwDonHang.DataSource = DonHangBUS.LoadDanhSachDonHangTheoMa(txtMaDonHang.Text);
            grwDonHang.DataBind();
        }

        protected void btnCapNhat_Click(object sender, EventArgs e)
        {
            DonHangDTO dh = new DonHangDTO();
            dh.MaDH = int.Parse(txtMaDonHang.Text);
            dh.TinhTrang = int.Parse(drpTinhTrang.SelectedValue);
            if (DonHangBUS.CapNhatTinhTrangDonHang(dh) != 0)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Cập Nhật Thành Công!');", true);
                LoadDanhSachDonHang();
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Cập Nhật Thất Bại!');", true);
            }     
        }

        protected void grwDonHang_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            DataTable dt = DonHangBUS.LoadDonHang(grwDonHang.Rows[e.NewSelectedIndex].Cells[1].Text);
            foreach (DataRow r in dt.Rows)
            {
                txtMaDonHang.Text = r["madh"].ToString();
                lblTaiKhoan.Text = r["username"].ToString();
                lblNguoiNhan.Text = r["tenkh"].ToString();
                lblDiaChi.Text = r["diachi"].ToString();
                lblDienThoai.Text = r["dienthoai"].ToString();
                lblEmail.Text = r["email"].ToString();
                lblTongTien.Text = r["tongtien"].ToString() + " VNĐ";
                lblNgayLap.Text = string.Format("{0:d}",r["ngaylap"]);
                lblNgayGiao.Text = string.Format("{0:d}", r["ngaylap"]);
                lblCongTyVanChuyen.Text = r["tenctvc"].ToString();
                if (r["trangthai"].ToString() == "1")
                {
                    lblTrangThai.Text = "Đã Giao";
                }
                else
                {
                    lblTrangThai.Text = "Đang Giao";
                }
                lblHinhThucGiao.Text = r["TenHTG"].ToString();
                drpTinhTrang.SelectedValue = r["tinhtrang"].ToString();
            }
                
        }

        protected void btnXemChiTiet_Click(object sender, EventArgs e)
        {
            if (txtMaDonHang.Text != "")
            {
                Response.Redirect("~/admin/XemChiTietDonHang.aspx?MADH=" + txtMaDonHang.Text);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Chưa Chọn Đơn Hàng!');", true);
            }
        }

        protected void btnTimKiemTheoNgay_Click(object sender, EventArgs e)
        {
            grwDonHang.DataSource = DonHangBUS.LoadDanhSachDonHangTheoNgay(drpNgay.SelectedValue+"/"+drpThang.SelectedValue+"/"+drpNam.SelectedValue);
            grwDonHang.DataBind();
        }
    }
}